package com.derive.education_service.web.registration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.derive.education_service.common.global.Paper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.Registration;
import com.derive.education_service.mapper.RegistrationMapper;
import com.derive.education_service.web.registration.bean.dto.InfoIntentionAddDto;
import com.derive.education_service.web.registration.bean.dto.QueryParam;
import com.derive.education_service.web.registration.service.RegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 信息登记逻辑操作
 */
@Service
@Slf4j
public class RegistrationServiceImpl implements RegistrationService {

    @Autowired
    private RegistrationMapper registrationMapper;

    /**
     * 信息登记
     * @param intentionAddDto
     * @return
     */
    @Override
    public ResponseMessage addInfoIntention(InfoIntentionAddDto intentionAddDto) {
        // 数据填充
        Registration registration = new Registration();
        BeanUtils.copyProperties(intentionAddDto, registration);

        // 默认为1
        registration.setTag(1);

        registration.setCreateTime(new Date());
        registration.setUpdateTime(new Date());

        // 新增
        int row = registrationMapper.insert(registration);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("信息登记异常{[addInfoIntention]}");
        } else {
            return ResponseMessage.createBySuccessMessage("登记成功");
        }
    }

    /**
     * 标记登记信息 -- 设置为已读 tag=0，默认为1
     * @param resId
     * @return
     */
    @Override
    public ResponseMessage tagInfoIntention(int resId) {
        Registration registration = new Registration();
        registration.setResId(resId);
        // 设置为已读
        registration.setTag(0);
        log.info("标记登记信息{[]}");
        int row = registrationMapper.updateById(registration);
        if (row <= 0) {
            return ResponseMessage.createByErrorMessage("标记登记信息异常{[tagInfoIntention]}");
        } else {
            return ResponseMessage.createBySuccessMessage("已设置为已读");
        }
    }

    /**
     * 删除登记信息 -- 物理删除
     * @param resId
     * @return
     */
    @Override
    public boolean delInfoIntention(int resId) {
        // 删除记录
        log.info("删除登记信息{[]}");
        int row = registrationMapper.deleteById(resId);
        return row <= 0 ? false : true;
    }

    /**
     * 查询登记详情
     * @param resId
     * @return
     */
    @Override
    public Registration getRegistrationById(int resId) {
        Registration registration = registrationMapper.selectById(resId);
        return registration;
    }

    /**
     * 查询登记列表信息 -- 分页查询 -- 条件查询 -- 时间倒序
     * @param queryParam
     * @return
     */
    @Override
    public Paper<Registration> getRegistrationList(QueryParam queryParam) {
        // 分页参数
        Page<Registration> page = new Page<>();
        page.setCurrent(queryParam.getCurrent());
        page.setSize(queryParam.getSize());

        // 构建查询条件
        QueryWrapper<Registration> queryWrapper = new QueryWrapper<>();
        if (queryParam.getUserName() != null && !queryParam.getUserName().isEmpty()) {
            queryWrapper.like("user_name", queryParam.getUserName());
        }
        if (queryParam.getGraduation() != null && !queryParam.getGraduation().isEmpty()) {
            queryWrapper.eq("graduation", queryParam.getGraduation());
        }
        if (queryParam.getTag() != null) {
            queryWrapper.eq("tag", queryParam.getTag());
        }
        // 时间倒序
        queryWrapper.orderByDesc("create_time");
        // 检索
        Page<Registration> registrationPage = registrationMapper.selectPage(page, queryWrapper);

        Paper<Registration> paper = new Paper<>();
        paper.setCurrent(registrationPage.getCurrent());
        paper.setSize(registrationPage.getSize());
        paper.setPages(registrationPage.getPages());
        paper.setTotal(registrationPage.getTotal());
        paper.setData(registrationPage.getRecords());

        return paper;
    }
}
